package me.lc.hammerheads.sprite;

import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.core.StageManager;
import me.lc.hammerheads.util.AudioHelper;

import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 工具条
 * 
 * @author Administrator
 * 
 */
public class ToolBar extends Node {
	Sprite bg;
	// 双倍威力锤子
	Label doubleCount;
	// 炸弹
	Label fireCount;
	// 加血
	Label heartCount;

	float shopStartX = ResolutionIndependent.resolveDp(3);;
	float shopEndX = ResolutionIndependent.resolveDp(40);

	float doubleStartX = ResolutionIndependent.resolveDp(62);
	float doubleEndX = ResolutionIndependent.resolveDp(101);

	float fireStartX = ResolutionIndependent.resolveDp(146);
	float fireEndX = ResolutionIndependent.resolveDp(185);

	float heartStartX = ResolutionIndependent.resolveDp(233);
	float heartEndX = ResolutionIndependent.resolveDp(269);

	static int FontSize = 25;

	public ToolBar() {
		this.setPosition(0, 0);
		this.setAnchorPercent(0, 0);

		bg = Sprite.make(GameSystem.tex_toolbarBg);
		bg.setAnchorPercent(0, 0);
		bg.setPosition(0, 0);
		this.addChild(bg);

		float y = 0;

		doubleCount = Label.make(StageManager.instance.doubleHammerCount + "",
				FontSize);
		doubleCount.setAnchorPercent(0, 0);
		doubleCount.setColor(WYColor3B.make(255, 255, 255));
		float doubleX = GameSystem.Screen_Width * 0.340625f;
		doubleCount.setPosition(doubleX, y);
		addChild(doubleCount);

		fireCount = Label.make(StageManager.instance.bombCount + "", FontSize);
		fireCount.setAnchorPercent(0, 0);
		fireCount.setColor(WYColor3B.make(255, 255, 255));
		float fireX = GameSystem.Screen_Width * 0.621875f;
		fireCount.setPosition(fireX, y);
		addChild(fireCount);

		heartCount = Label.make(StageManager.instance.addHeartCount + "",
				FontSize);
		heartCount.setAnchorPercent(0, 0);
		heartCount.setColor(WYColor3B.make(255, 255, 255));
		float heartX = GameSystem.Screen_Width * 0.884375f;
		heartCount.setPosition(heartX, y);
		addChild(heartCount);
	}

	public void processTouch(WYPoint point) {
		if (GameManager.instance.isPaused) {
			return;
		}
		if (point.x >= shopStartX && point.x <= shopEndX) {
			AudioHelper.buttonClick();
			GameManager.instance.pauseGame();
		} else if (point.x >= doubleStartX && point.x <= doubleEndX) {
			AudioHelper.buttonClick();
			if (StageManager.instance.doubleHammerCount > 0) {
				GameManager.instance.addDoubleHammerCount(20);
				StageManager.instance.doubleHammerCount--;
				// 更新界面
				doubleCount.setText(StageManager.instance.doubleHammerCount
						+ "");
				StageManager.instance.saveToolsCount();
			} else {
				GameManager.instance.pauseGame();
			}

		} else if (point.x >= fireStartX && point.x <= fireEndX) {
			if (StageManager.instance.bombCount > 0) {
				GameManager.instance.bombAllDwarf();
				AudioHelper.boom();
				StageManager.instance.bombCount--;
				// 更新界面
				fireCount.setText(StageManager.instance.bombCount + "");
				StageManager.instance.saveToolsCount();
			} else {
				GameManager.instance.pauseGame();
			}

		} else if (point.x >= heartStartX && point.x <= heartEndX) {
			// 无尽模式不能用
			if (StageManager.instance.addHeartCount > 0
					&& GameManager.instance.gameType == 1) {
				// GameManager.instance.changeGameHeart(true);
				// 补满血
				GameManager.instance.fullHeart();
				GameManager.instance.setAddedHeart(true);
				AudioHelper.addHeart();
				StageManager.instance.addHeartCount--;
				// 更新界面
				heartCount.setText(StageManager.instance.addHeartCount + "");
				StageManager.instance.saveToolsCount();
			} else {
				GameManager.instance.pauseGame();
			}
		}
	}

	public void updateToolCount() {
		doubleCount.setText(StageManager.instance.doubleHammerCount + "");
		fireCount.setText(StageManager.instance.bombCount + "");
		heartCount.setText(StageManager.instance.addHeartCount + "");
	}
}
